package com.agileai.pm.module.proc.handler;

import java.util.Date;
import java.util.List;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.StandardListHandler;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.pm.cxmodule.ProcProjectTrackManage;
import com.agileai.pm.domain.Consts;
import com.agileai.pm.domain.Project;
import com.agileai.util.DateUtil;
import com.agileai.util.StringUtil;

public class ProcProjectTrackManageListHandler
        extends StandardListHandler {
    public ProcProjectTrackManageListHandler() {
        super();
        this.editHandlerClazz = ProcProjectTrackManageEditHandler.class;
        this.serviceId = buildServiceId(ProcProjectTrackManage.class);
    }

    protected void processPageAttributes(DataParam param) {
    	
    }

    protected void initParameters(DataParam param) {
    	Date lastMonthDate = DateUtil.getDateAdd(new Date(), DateUtil.MONTH, -1);
    	lastMonthDate = DateUtil.getBeginOfMonth(lastMonthDate);
    	initParamItem(param, "pptStartDate", DateUtil.getDateByType(DateUtil.YYMMDD_HORIZONTAL, lastMonthDate));
    	
    	String t = param.getString("pptDate");
    	if(StringUtil.isNullOrEmpty(t)){
    		String date=DateUtil.getDateByType(DateUtil.YYMMDD_HORIZONTAL, new Date());
    		param.put("pptDate", date);
    	}else{
    		initParamItem(param, "pptDate", "");
    	}
    	
    	User user=(User)this.getUser();
    	String weekNum= param.getString("weekNum");
    	if(StringUtil.isNullOrEmpty(weekNum)){
    		DataRow userRow=getService().getSysUserInfo(new DataParam("userCode",user.getUserCode()));
    		int num=userRow.getInt("USER_TRACK_SHOWNUM");
    		if(num == 0){
    			num=5;
    		}
    		param.put("weekNum", num);
    	}else{
    		param.put("weekNum", Integer.valueOf(weekNum));
    		DataParam userParam=new DataParam();
    		userParam.put("userCode",user.getUserCode());
    		userParam.put("weekNum",Integer.parseInt(weekNum));
    		getService().updateUserTrankNum(userParam);
    	}
    }

    protected ProcProjectTrackManage getService() {
        return (ProcProjectTrackManage) this.lookupService(this.getServiceId());
    }
    public ViewRenderer prepareDisplay(DataParam param){
		Project project = (Project)this.getSessionAttribute(Consts.ProjectSessionKey);
		String projectId=project.getProjectId();
		if(StringUtil.isNullOrEmpty(projectId)){
			this.setAttribute("projectId", projectId);
			param.put("projectId",projectId);
		}

    	mergeParam(param);
		initParameters(param);
		this.setAttributes(param);
		param.put("projectId",projectId);
		List<DataRow> rsList = getService().findRecords(param);
		for(DataRow row:rsList){
			String pptId=row.getString("PPT_ID");  
			String pwiName=row.getString("PWI_NAME"); 
			
			int trackNameLength = pwiName.length();
			pwiName ="<p>"+pwiName.substring(0, trackNameLength-29)+"</p><p>"+pwiName.substring(trackNameLength-29,trackNameLength-3)+"</p>";
			row.put("PWI_NAME", pwiName);
			
			List<DataRow> trackList=getService().findprocTracks(pptId);
			row.put("trackList", trackList);
			row.put("trackListSize", trackList.size());
		}
		this.setRsList(rsList); 
		processPageAttributes(param);
		return new LocalRenderer(getPage());
	}
    
    @PageAction
    public ViewRenderer saveWeekInfo(DataParam param) {
    	String responseText = "success";
    	String pptDescribe=param.getString("TW_CONTENT");
    	String pptId=param.getString("pptId");
    	DataParam WeekInfoParam=new DataParam("pptDescribe",pptDescribe);
    	WeekInfoParam.put("pptId",pptId);
    	int trackSize=param.getInt("trackSize");
    	getService().updateWeekInfo(WeekInfoParam);
    	
    	for(int i=0;i<trackSize;i++){
    		String ptdSummary=param.getString("ptdSummary_"+i);
        	String ptdId=param.getString("ptdId_"+i);
        	DataParam TrackParam=new DataParam();
        	TrackParam.put("ptdId", ptdId);
        	TrackParam.put("ptdSummary", ptdSummary);
        	getService().updateTrackInfo(TrackParam);
    	}
        return new AjaxRenderer(responseText);
    }
    @PageAction
    public ViewRenderer subimtWeekInfo(DataParam param) {
    	String responseText = "success";
    	String pptId=param.getString("pptId");
    	DataParam pptStaeParam=new DataParam();
    	pptStaeParam.put("pptState", "TRACK_SUMBIT");
    	pptStaeParam.put("pptId", pptId);
    	getService().updatePptstate(pptStaeParam);
        return new AjaxRenderer(responseText);
    }
    
    @PageAction
    public ViewRenderer setWeekNum(DataParam param) {
    	String responseText = "success";
    	String pptId=param.getString("pptId");
    	DataParam pptStaeParam=new DataParam();
    	pptStaeParam.put("pptState", "TRACK_SUMBIT");
    	pptStaeParam.put("pptId", pptId);
    	getService().updatePptstate(pptStaeParam);
        return new AjaxRenderer(responseText);
    }
}
